﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Rem.Objects;
using FileDB;


namespace Rem.Objects.Forms
{
    public partial class ObjectPickerForm : Form
    {
        public BaseObjectTypes ObjectType { get; set; }
        public ObjectPickerForm(BaseObjectTypes objectType)
        {
            InitializeComponent();
            this.ObjectType = objectType;
            this.objectDataGridView.MultiSelect = false;
            this.Text = string.Format("请选择{0}", QueryObject.BaseObjectInfo[objectType]);
            
            switch (this.ObjectType)
            {
                case BaseObjectTypes.Employee:
                    UcDataGrid.InitializateGridView(this.objectDataGridView,
                        RemDatabaseEntities.Employees,
                        QueryConstants.EmployeeFieldNameHeadTextMapping,
                        QueryConstants.EmployeeInvisiableFieldNames);
                    break;
                case BaseObjectTypes.Contract:
                    UcDataGrid.InitializateGridView(this.objectDataGridView,
                       RemDatabaseEntities.Contracts,
                       QueryConstants.ContractFieldNameHeadTextMapping,
                       QueryConstants.ContractInvisiableFieldNames);
                    break;
                case BaseObjectTypes.ContractQuery:
                    break;
                case BaseObjectTypes.Customer:
                    UcDataGrid.InitializateGridView(this.objectDataGridView,
                     RemDatabaseEntities.Customers,
                     QueryConstants.CustomerFieldNameHeadTextMapping,
                     QueryConstants.CustomerInvisiableFieldNames);
                    break;
                case BaseObjectTypes.CustomerQuery:
                    break;
                case BaseObjectTypes.Reminder:
                    UcDataGrid.InitializateGridView(this.objectDataGridView,
                        RemDatabaseEntities.Reminders,
                        QueryConstants.ReminderFieldNameHeadTextMapping,
                        QueryConstants.ReminderInvisiableFieldNames);
                    break;
                case BaseObjectTypes.ReminderQuery:
                    break;
                case BaseObjectTypes.StartInfo:
                    break;
                case BaseObjectTypes.Queries:
                    break;
                case BaseObjectTypes.QueryClauses:
                    break;
                case BaseObjectTypes.WorkLog:
                    break;
                case BaseObjectTypes.None:
                    break;
                default:
                    break;
            }

        }

        public Object SelectedObject { get; set; }
        public List<Object> SelectedObjects { get; set; }

        private void btrnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            if (objectDataGridView.SelectedRows.Count == 1)
            {
                this.SelectedObject = objectDataGridView.SelectedRows[0].DataBoundItem;
            }
            else if (objectDataGridView.SelectedRows.Count > 1)
            {
                this.SelectedObjects = new List<object>();
                foreach (DataGridViewRow row in objectDataGridView.SelectedRows)
                {
                    this.SelectedObjects.Add(row.DataBoundItem);
                }
            }
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        private void dataGridView1_DoubleClick(object sender, EventArgs e)
        {
            if (this.objectDataGridView.SelectedRows.Count == 1)
            {
                btnConfirm_Click(sender, e);
            }
        }
    }
}
